GLOBAL.setmetatable(env, { __index = function(t, k) return GLOBAL.rawget(GLOBAL, k) end })

local function inv(oldFunc)
    return function(a, b, ...)
        local action = b ~= nil and b or a
        local invobj = action.invobject
        action.invobject = nil
        local result = oldFunc(a, b, ...)
        action.invobject = invobj
        return result
    end
end

local GetUpvalue = function(fn, upvalname)
    local i = 1
    while true do
        local name, value = debug.getupvalue(fn, i)
        if not name then break end
        if name == upvalname then
            return value, i
        end
        i = i + 1
    end
end

local ActionCanMapSoulhop, idx = GetUpvalue(ACTIONS.BLINK_MAP.fn, "ActionCanMapSoulhop")
debug.setupvalue(ACTIONS.BLINK_MAP.fn, idx, inv(ActionCanMapSoulhop))
ACTIONS.BLINK_MAP.stroverridefn = inv(ACTIONS.BLINK_MAP.stroverridefn)
------------------ 主机 ---------------------------
AddStategraphPostInit("wilson", function(sg)
    local oldHandler = sg.actionhandlers[ACTIONS.BLINK_MAP].deststate
    sg.actionhandlers[ACTIONS.BLINK_MAP].deststate = inv(oldHandler)
end)

------------------ 客机 ---------------------------
AddStategraphPostInit("wilson_client", function(sg)
    local oldHandler = sg.actionhandlers[ACTIONS.BLINK_MAP].deststate
    sg.actionhandlers[ACTIONS.BLINK_MAP].deststate = inv(oldHandler)
end)
